package com.foreknow.dao.impl;

import com.foreknow.bean.User;
import com.foreknow.dao.UserDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public boolean save(User user) throws SQLException {
        String sql = "insert into users (adminname, password) values (?,?)";
        int isRight = jdbcTemplate.update(sql, user.getAdminName(), user.getPassword());
        return isRight == 1;
    }

    @Override
    public User login(String name, String password) throws SQLException{
        String sql = "select * from users where adminname = ? and password = ?";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.USER_MAPPING);
        List<Object> list = jdbcTemplate.query(sql, mapping, name, password);
        if (list.size()>0){
            User user = (User) list.get(0);
            return user;
        }
        return null;
    }
}
